package com.zsc.wxapp.services.impl;

import com.zsc.wxapp.entity.Announcement;
import com.zsc.wxapp.entity.dto.AdminAnnouncementDTO;
import com.zsc.wxapp.entity.dto.AnnouncementDTO;
import com.zsc.wxapp.entity.vo.AdminAnnouncementVO;
import com.zsc.wxapp.entity.vo.AnnouncementListVO;
import com.zsc.wxapp.entity.vo.AnnouncementVO;
import com.zsc.wxapp.mapper.AnnouncementMapper;
import com.zsc.wxapp.mapper.CigaretteMapper;
import com.zsc.wxapp.result.Result;
import com.zsc.wxapp.services.AnnouncementService;
import com.zsc.wxapp.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDate;
import java.util.*;

@Service
@Slf4j
public class AnnouncementServiceImpl implements AnnouncementService {

    @Resource
    private AnnouncementMapper announcementMapper;
    @Resource
    private CigaretteMapper cigaretteMapper;
    @Resource
    private AliOssUtil aliOssUtil;
    /**
     * 根据key获取该分类的所有公告
     * @param type 分类key
     * @return List
     */
    @Override
    public List<AnnouncementListVO> getByKey(Integer type) {
        List<Announcement> list = announcementMapper.getByKey(type);
        List<AnnouncementListVO> result = new ArrayList<>();

        for (Announcement announcement : list) {
            AnnouncementListVO a = new AnnouncementListVO();
            BeanUtils.copyProperties(announcement, a);
            result.add(a);
        }
        return result;
    }

    /**
     * 根据卷烟代码获取公告
     * @param productCode 卷烟代码
     * @return AnnouncementVO
     */
    @Override
    public List<AnnouncementVO> getByCigaretteId(String productCode) {
        List<Announcement> list = announcementMapper.getByProductCode(productCode);
        if (list.isEmpty()) {
            return null;
        }
        List<AnnouncementVO> result = new ArrayList<>();
        for (Announcement a : list) {
            AnnouncementVO announcementVO = new AnnouncementVO();
            BeanUtils.copyProperties(a, announcementVO);
            result.add(announcementVO);
        }
        return result;
    }

    /**
     * 获取公告详情 首页使用
     * @param id 公告id
     * @return AnnouncementVO
     */
    @Override
    public AnnouncementVO getById(Integer id) {
        Announcement announcement = announcementMapper.getById(id);
        if (announcement != null) {
            List<String> images = null;
            if (announcement.getImage() != null) {
                String[] urls = announcement.getImage().split("~");
                images = Arrays.asList(urls);
            }
            AnnouncementVO result = AnnouncementVO.builder()
                    .id(announcement.getId())
                    .type(announcement.getType())
                    .keyword1(announcement.getKeyword1())
                    .keyword2(announcement.getKeyword2())
                    .title(announcement.getTitle())
                    .content(announcement.getContent())
                    .image(images)
                    .createTime(announcement.getCreateTime())
                    .build();
            return result;
        }
        return null;
    }

    /**
     * 添加公告
     * @param announcementDTO
     * @return
     */
    @Override
    public String add( AnnouncementDTO announcementDTO) throws IOException {
        String image = null;

        if (announcementDTO.getFiles() != null) {
            List<String> images = new ArrayList<>();
            for (MultipartFile file : announcementDTO.getFiles()) {
//                try {
//                    //原始文件名
//                    String originalFilename = file.getOriginalFilename();
//                    //截取原始文件名的后缀
//                    String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
//                    //构建新的文件名
//                    String objectName = UUID.randomUUID() + extension;
//                    //上传文件并保存url
//                    images.add(aliOssUtil.upload(file.getBytes(), objectName));
//                } catch (Exception e) {
//                    log.error("文件上传失败:{}", e);
//                    return "文件上传失败";
//                }
                String base64Img = Base64.getEncoder().encodeToString(file.getBytes());
                images.add(base64Img);
            }

            image = String.join("~", images);
        }


        Announcement result = Announcement.builder()
                .cigaretteCode(announcementDTO.getCigaretteCode())
                .type(announcementDTO.getType())
                .keyword1(announcementDTO.getKeyword1())
                .keyword2(announcementDTO.getKeyword2())
                .title(announcementDTO.getTitle())
                .content(announcementDTO.getContent())
                .image(image)
                //.image(announcementDTO.getImage())
                .publisher("超级管理员")
                .createTime(LocalDate.now())
                .updateTime(LocalDate.now())
                        .build();

        //添加公告
        announcementMapper.add(result);
        return null;
    }

    @Override
    public String delete(List<Integer> ids) {
        return announcementMapper.delete(ids);
    }

    @Override
    public List<AnnouncementListVO> get() {
        List<Announcement> list = announcementMapper.getAll();
        List<AnnouncementListVO> result = new ArrayList<>();

        for (Announcement announcement : list) {
            AnnouncementListVO a = new AnnouncementListVO();
            BeanUtils.copyProperties(announcement, a);
            result.add(a);
        }
        return result;
    }

    @Override
    public AdminAnnouncementVO getListById(Integer id) {
        Announcement announcement = announcementMapper.getById(id);
        if (announcement != null) {
            List<String> images = null;
            if (announcement.getBaseImg() != null) {
                String[] urls = announcement.getBaseImg().split("~");
                images = Arrays.asList(urls);
            }
            AdminAnnouncementVO result = AdminAnnouncementVO.builder()
                    .id(announcement.getId())
                    .type(announcement.getType())
                    .keyword1(announcement.getKeyword1())
                    .keyword2(announcement.getKeyword2())
                    .title(announcement.getTitle())
                    .content(announcement.getContent())
                    .image(images)
                    .createTime(announcement.getCreateTime())
                    .build();
            return result;
        }
        return null;
    }

    @Override
    public String update(AdminAnnouncementDTO announcementDTO) throws IOException {
        String image = null;

        if (announcementDTO.getFiles() != null) {
            List<String> images = new ArrayList<>();
            for (MultipartFile file : announcementDTO.getFiles()) {
                String base64Img = Base64.getEncoder().encodeToString(file.getBytes());
                images.add(base64Img);
            }

            image = String.join("~", images);
        }


        Announcement result = Announcement.builder()
                .id(announcementDTO.getId())
                .cigaretteCode(announcementDTO.getCigaretteCode())
                .type(announcementDTO.getType())
                .keyword1(announcementDTO.getKeyword1())
                .keyword2(announcementDTO.getKeyword2())
                .title(announcementDTO.getTitle())
                .content(announcementDTO.getContent())
                .image(image)
                //.image(announcementDTO.getImage())
                .publisher("超级管理员")
                .createTime(LocalDate.now())
                .updateTime(LocalDate.now())
                .build();

        //添加公告
        announcementMapper.update(result);
        return null;
    }


}
